#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :02采招网练习.py
# @Time      :2024/4/14 12:48
# @Author    :Sherlock
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
import requests
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
from base64 import b64decode


def decrypt_aes_data(encrypted_str, key_dict, iv_dict):
    # 将 CryptoJS 的 WordArray 转换为 Python 的字节串
    key = bytes([item for sublist in key_dict['words'] for item in sublist.to_bytes(4, byteorder='big')])
    iv = bytes([item for sublist in iv_dict['words'] for item in sublist.to_bytes(4, byteorder='big')])

    # 确保 key 和 iv 的长度正确
    assert len(key) == 16, "Key must be 16 bytes long"
    assert len(iv) == 16, "IV must be 16 bytes long"

    # 解码 base64 编码的字符串
    encrypted_bytes = b64decode(encrypted_str)

    # 创建一个 AES 解密对象
    cipher = AES.new(key, AES.MODE_CBC, iv=iv)

    # 执行解密
    decrypted_bytes = cipher.decrypt(encrypted_bytes)

    # decrypted_text = unpad(decrypted_bytes, 16)

    return decrypted_bytes.decode()


headers = {
    'accept': 'text/plain, */*; q=0.01',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'origin': 'https://search.bidcenter.com.cn',
    'referer': 'https://search.bidcenter.com.cn/',
    'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
}

data = {
    'from': '6137',
    'guid': 'f08093dc-00b3-4c30-95ab-b5d0b38e54af',
    'location': '6138',
    'token': '',
    'next_token': '',
    'keywords': '%E6%9C%8D%E5%8A%A1%E5%99%A8',
    'mod': '0',
    'page': '2',
}
variate = {
    "key": {
        "words": [
            863652730,
            2036741733,
            1164342596,
            1782662963
        ],
        "sigBytes": 16
    },
    "aceIV": {
        "words": [
            1719227713,
            1314533489,
            1397643880,
            1749959510
        ],
        "sigBytes": 16
    }
}
response = requests.post('https://interface.bidcenter.com.cn/search/GetSearchProHandler.ashx', headers=headers,
                         data=data)
decrypted_text = decrypt_aes_data(response.text.encode(), variate['key'], variate['aceIV'])
print(decrypted_text.strip())
